//
//  FaceIDViewController.m
//  商城
//
//  Created by runone02 on 2018/9/5.
//  Copyright © 2018年 liqiyun. All rights reserved.
//

#import "FaceIDViewController.h"

#import <LocalAuthentication/LocalAuthentication.h>

@interface FaceIDViewController ()

@property (nonatomic, strong) LAContext *context;

@end

@implementation FaceIDViewController

- (LAContext *)context {
    if (_context == nil) {
        _context = [LAContext new];
    }
    return _context;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [loginBtn setTitle:@"Login by FaceID😀" forState:UIControlStateNormal];
    [loginBtn sizeToFit];
    loginBtn.center = self.view.center;
    [loginBtn addTarget:self action:@selector(loginBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginBtn];
}

- (void)loginBtnClick {
    
    NSError *error;
    
    if (@available(iOS 9.0, *)) {
        BOOL canAuthentication = [self.context canEvaluatePolicy:LAPolicyDeviceOwnerAuthentication error:&error];
        
        if (canAuthentication) {
            
            [self.context evaluatePolicy:LAPolicyDeviceOwnerAuthentication localizedReason:@"验证FaceID" reply:^(BOOL success, NSError * _Nullable error) {
                
                //注意iOS 11.3之后需要配置Info.plist权限才可以通过Face ID验证哦!不然只能输密码啦...
                if (success) {
                    UIAlertController *alearC = [UIAlertController alertControllerWithTitle:@"Success" message:nil preferredStyle:UIAlertControllerStyleAlert];
                    [alearC addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    }]];
                    [self presentViewController:alearC animated:YES completion:nil];
                    
                } else {
                    NSLog(@"%@",error);
                }
            }];
        }
    } else {
        // Fallback on earlier versions
        NSLog(@"ios9之前");
    }
}

@end
